﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using DGSFramework.Models;
using System.Web.Security;

namespace Framework
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public DGSFrameworkEntities entity = new DGSFrameworkEntities();
        public List<udv_Categories> allCate = new List<udv_Categories>();
        public List<udv_Articles> allArt = new List<udv_Articles>();
        public dgs_StockDatas allStockData = new dgs_StockDatas();
        public udv_Users udvUsers = new udv_Users();
        MembershipUser currentUser;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCacheArticle();
                //LoadArticles();
                var currentDate = entity.dgs_StockDatas.Max(d => d.GetTime);
                LoadTopVolume(currentDate);
                LoadTopGainer(currentDate);
                LoadTopActive(currentDate);
                LoadArticleLang();
                // LoadTopCommentary();
                LoadMemberData();
            }
        }

        public void LoadCacheArticle()
        {
            if (Cache["Article"] == null)
            {
                Cache["Article"] = allArt = entity.udv_Articles.Include("udv_Categories").ToList();
            }
            else
            {
                allArt = (List<udv_Articles>)Cache["Article"];
            }
        }

        public void LoadArticles()
        {
            try
            {
                //var lstTopArticle = allArt.OrderBy(ar => ar.DisplayOrder).ThenByDescending(ar => ar.AddedDate).Take(4).ToList();
                var lstTopArticle = (from a in entity.udv_Articles
                          join b in entity.udv_Categories on a.udv_Categories.CateID equals b.CateID
                          where b.IsTop == true
                          select new
                          {
                              a.ArticleID,
                              a.Title,
                              a.Title_en,
                              a.Abstract,
                              a.Abstract_en,
                              a.UrlRewrite,
                              a.ImageUrl,
                              a.UpdatedDate,
                              a.AddedDate,
                              a.DisplayOrder
                          }).OrderBy(d => d.DisplayOrder).ThenByDescending(d => d.AddedDate).Take(4).ToList();
                rptHotNews.DataSource = lstTopArticle;
                rptHotNews.DataBind();
            }
            catch (Exception ex)
            {

                throw;
            }

        }
        public void LoadArticleLang()
        {
            try
            {
                if (DGSFramework.Globals.IsLanguageVN() == true)
                {
                    int catevi = 30;
                    var lstArtVn = allArt.Where(avn => avn.udv_Categories.CateID == catevi).OrderBy(avn => avn.DisplayOrder)
                           .ThenByDescending(avn => avn.AddedDate).Take(4).ToList();
                    rptHotNews.DataSource = lstArtVn;
                    rptHotNews.DataBind();

                    imgSlide3.ImageUrl = "/Images/slide3.png";
                    imgSlide3.Height = 350;
                }
                else
                {
                    int cateen = 31;
                    var cateEn = allCate.Where(en => en.CateID == cateen).FirstOrDefault();
                    var lstArtEn = allArt.Where(aen => aen.udv_Categories.CateID == cateen).OrderBy(aen => aen.DisplayOrder)
                           .ThenByDescending(aen => aen.AddedDate).Take(4).ToList();
                    rptHotNews.DataSource = lstArtEn;
                    rptHotNews.DataBind();

                    imgSlide3.ImageUrl = "/Images/slide3_en.png";
                    imgSlide3.Height = 350;


                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void LoadTopVolume(DateTime? currentDate)
        {
            try
            {               
                var hometopvolume = entity.dgs_StockDatas.Where(d => d.GetTime == currentDate && d.dgs_Stocks.IsDelete == false).OrderByDescending(d => d.TongKL).Take(15).Select(d => new StockDataModel { Name = d.dgs_Stocks.Code });
                rptHomeTopVolume.DataSource = hometopvolume;
                rptHomeTopVolume.DataBind();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void LoadTopGainer(DateTime? currentDate)
        {
            try
            {                
                var hometopgainer = entity.dgs_StockDatas.Where(d => d.GetTime == currentDate && d.dgs_Stocks.IsDelete == false).OrderByDescending(d => (d.Cao / d.GiaThamChieu - 1) * 100).Skip(0).Take(15).Select(d => new StockDataModel { Name = d.dgs_Stocks.Code});
                rptHomeTopGainer.DataSource = hometopgainer;
                rptHomeTopGainer.DataBind();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void LoadTopActive(DateTime? currentDate)
        {
            try
            {               
                var stockList = new List<StockDataModel>();
                var lstStock = entity.dgs_StockDatas.Where(d => d.GetTime == currentDate && (d.BT > 0 || d.ST > 0) && d.dgs_Stocks.IsDelete == false)
                    .OrderByDescending(d => (d.BT + d.ST)).Skip(0).Take(15).Select(d => new StockDataModel { Name = d.dgs_Stocks.Code }).ToList();
                 rptHomeTopActive.DataSource = lstStock;
                rptHomeTopActive.DataBind();
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void btnMemberLogout_Click(object sender, EventArgs e)
        {
            try
            {
                currentUser = Membership.GetUser();
                if (currentUser != null)
                {
                    FormsAuthentication.SignOut();
                    Response.Redirect("/Default.aspx");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void btnLoginHome_ServerClick(object sender, EventArgs e)
        {
            try
            {
                //check member
                string mem = tbMemberID.Text;
                string pass = tbMemberPW.Text;
                var getmem = entity.udv_Users.Where(a => a.UserName == mem && (a.ExpiredDate >= DateTime.Now)).FirstOrDefault();
                if (getmem != null && Membership.ValidateUser(mem, pass))
                {
                    //Session["objMember"] = getmem;
                    FormsAuthentication.SetAuthCookie(mem, true);
                    frlog.Visible = false;
                    divdatamem.Visible = true;
                    lblUserNameMember.Text = getmem.UserName;
                    lblEmailMember.Text = getmem.Email;
                }
                else
                {
                    lblAlertMember.Text = "Vui Lòng Kiểm Tra Tài Khoản !";
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void LoadMemberData()
        {
            try
            {
                currentUser = Membership.GetUser();
                if (currentUser != null)
                {
                    if (currentUser.IsApproved == true)
                    {
                        frlog.Visible = false;
                        divdatamem.Visible = true;
                        lblUserNameMember.Text = currentUser.UserName;
                        lblEmailMember.Text = currentUser.Email;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}